package edu.study.common.retry;

import lombok.AllArgsConstructor;

import java.math.BigDecimal;
import java.util.Date;

@AllArgsConstructor
public class PowPolicy implements RetryPolicy {

    private BigDecimal powM = new BigDecimal(1.01);

    public PowPolicy() {}

    @Override
    public Date nextRetryDate(Date now, int alreadyTryTimes) {
        long next = (new BigDecimal(now.getTime()).multiply(power(powM, (alreadyTryTimes + 1)))).longValue() ;

        return new Date(next);
    }

    @Override
    public Date nextRetryDate(Date now, int alreadyTryTimes, long baseInterval) {
        long next = (new BigDecimal(now.getTime()).multiply(power(powM, (alreadyTryTimes + 1)))).longValue() ;

        return new Date(next);
    }

    @Override
    public Date nextRetryDate(Date now, int alreadyTryTimes, long baseInterval, BigDecimal basePow) {
        long next = (new BigDecimal(now.getTime()).multiply(power(basePow, (alreadyTryTimes + 1)))).longValue() ;

        return new Date(next);
    }

    public BigDecimal power(BigDecimal a , int b) {
        BigDecimal power = new BigDecimal(1);
        for (int c = 0; c < b; c++)
            power = power.multiply(a);
        return power;
    }
}
